package com.genius.controller;

import cn.hutool.json.JSONUtil;
import com.genius.annotation.PreFuncAuth;
import com.genius.annotation.PreInnerAuth;
import com.genius.core.JSONResult;
import com.genius.monitor.bo.DeleteLoginLogByIdsBO;
import com.genius.monitor.bo.SelectLoginLogByPageBO;
import com.genius.monitor.pojo.LoginLog;
import com.genius.service.LoginLogService;
import com.genius.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("loginLog")
@Api(value = "登陆日志接口", tags = {"用于登陆日志的接口"})
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    @PostMapping("/selectLoginLogByPage")
    @ApiOperation(value = "登陆日志",notes = "登陆日志", httpMethod = "POST")
    @PreFuncAuth("MONITOR:LOGINLOG:SELECT")
    public JSONResult selectLoginLogByPage(@Valid @RequestBody SelectLoginLogByPageBO selectLoginLogByPageBO){
        int pageNum = selectLoginLogByPageBO.getPageNum();
        int pageSize = selectLoginLogByPageBO.getPageSize();
        LoginLog loginLog = JSONUtil.parse(selectLoginLogByPageBO).toBean(LoginLog.class);
        PageUtils.startPage(pageNum,pageSize);
        List<LoginLog> selectScheduleJobLogList = loginLogService.selectLoginLogByPage(loginLog);
        Map<String,Object> result = PageUtils.getResultTableData(selectScheduleJobLogList);
        return JSONResult.ok(result);
    }


    @PostMapping("/insertLoginLog")
    @PreInnerAuth
    public JSONResult insertLoginLog(@Valid @RequestBody LoginLog loginLog) {
        int rows = loginLogService.insertLoginLog(loginLog);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    @PostMapping("/deleteLoginLogByIds")
    @ApiOperation(value = "删除登陆日志",notes = "删除登陆日志", httpMethod = "POST")
    @PreFuncAuth("MONITOR:LOGINLOG:DELETE")
    public JSONResult deleteLoginLogByIds(@Valid @RequestBody DeleteLoginLogByIdsBO deleteLoginLogByIdsBO) {
        int rows = loginLogService.deleteLoginLogByIds(deleteLoginLogByIdsBO.getLoginLogIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
